layui.use(['table', 'admin', 'ax', 'func','tree','ztree','layer','form','upload'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    //var admin = layui.admin;
    var func = layui.func;
    var tree = layui.tree;
    var $ZTree = layui.ztree;
    var layer = layui.layer;
    var form = layui.form;
    var upload = layui.upload;

    /**
     * 网上建店信息gcg_wsjd管理
     */
    var GcgWsjd = {
        tableId: "gcgWsjdTable"
    };

    /**
     * 检索营运经理
     */
    var languageAjax = new $ax(Feng.ctxPath + "/btyTxl/yyjlList");
    var languageResult = languageAjax.start();
    $.each(languageResult, function( index, val ) {
        $("#dm").append("<option value='"+val.yytel+"'>"+val.yyjl+"</option>");
    } );
    form.render('select');
    /**
     * 初始化表格的列
     */
    GcgWsjd.initColumn = function () {
        return [[
            {type: 'checkbox',LAY_CHECKED:true,hide:true},
            //{type: 'checkbox',fixed:'left'},
            {field: 'gcgWsjdId', hide: true, title: ''},
            //{field: 'yjzt', hide: true, title: ''},
            //{field: 'gcgWsjdYinhangkaId', hide: true, title: ''},
            {align: 'center', type: "numbers", title: '序号',fixed:'left'},
            //{field: 'gcgProjectId', hide: true, title: ''},
            {field: 'tiancai', title: '天财店名'},
            {field: 'yaoma', sort: true, title: '妖码'},
            {field: 'yyjl', title: '营运经理'},
            {field: 'dm', title: '店名'},
            {field: 'mtmingcheng', title: '美团店名'},
            {field: 'elememingcheng', title: '饿了么店名'},
            {align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 200}
        ]];
    };


    /**
     * 点击查询按钮
     */
    GcgWsjd.search = function () {
        var queryData = {};

        queryData['yaoma'] = $('#yaoma').val();
        queryData['mingcheng'] = $('#mingcheng').val();
        // queryData['yyjltel'] =$("#dm").val();//$("#dm  option:selected").val();
        var yyjl = $("#dm").find("option:selected").text();
        if (yyjl == '全部'){
            yyjl = '';
        }
        queryData['yyjl'] = yyjl;

        table.reload(GcgWsjd.tableId, {
            where: queryData, page: {curr: 1}
        });
    };
    form.on('select(dmss)', function (data) {
        GcgWsjd.search();
    });

    // 清空按钮点击事件
    $('#btnReset').click(function () {
        GcgWsjd.reset();
    })
    /**
     * 点击清空按钮
     */
    GcgWsjd.reset = function () {
        $("#mingcheng").val('');
        $("#yaoma").val('');
        $("#dm").val('');
        form.render();
        GcgWsjd.search();
    };


    // 渲染表格
    var tableResult = table.render({
        elem: '#' + GcgWsjd.tableId,
        url: Feng.ctxPath + '/btyTxl/wmList',
        page: true,
        height: "full-100",
        cellMinWidth: 100,
        cols: GcgWsjd.initColumn(),
        done: function (res, curr, count) {
            $('.title-tr').remove()
            // if (res.data == '' ){
            //     $('.layui-table-box').css('overflow','auto')
            //     $('.layui-table-header, .layui-table-body').css('overflow','visible')
            // }
            $(".laytable-cell-1-0-11").css('overflow','visible');

            //$('.layui-table thead tr').before('<tr class="title-tr"><td></td><th colspan="7" style="text-align: center;" class="jbth"><span class="jb">基本信息</span></th><th colspan="3" style="text-align: center;">金融信息</th><th colspan="4" class="th"></th></tr>')
        }
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        GcgWsjd.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

        GcgWsjd.openAddDlg();

    });

    // 导出excel
    $('#btnExp').click(function () {
        GcgWsjd.exportExcel();
    });

    $('#btnSend').click(function () {
        GcgWsjd.sendEmail();
    });


    /**
     * 弹出添加通知
     */
    GcgWsjd.openAddDlg = function () {
        parent.layer.open({
            type: 2,
            title: '新增',
            area: ['40%', '550px'],
            content: Feng.ctxPath + '/btyTxl/waimaiAdd',
            end: function () {
                GcgWsjd.search();
            },
            cancel:function (index){
            }
        });
    };

    /**
     * 导出excel按钮
     */
    GcgWsjd.exportExcel = function () {
        //获取导出的数据
        var ajax = new $ax(Feng.ctxPath + "/btyTxl/wmExportList");
        var mingcheng = $("#mingcheng").val();
        // var dm = $("#dm").val();
        var dm = $("#dm").find("option:selected").text();
        if (dm == '全部'){
            dm = '';
        }

        ajax.set("mingcheng",mingcheng);
        // ajax.set("yyjltel",dm);
        ajax.set("yyjl",dm);
        var result = ajax.start();
        var checkRows = table.checkStatus(GcgWsjd.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            //身份证银行卡导出excel科学计数法问题
            var lists=result;
            for(index in lists){
                var yaoma= lists[index].yaoma;
                lists[index].yaoma= yaoma+"\t";
            }
            table.exportFile(tableResult.config.id, lists, 'xls');
        }
    };


    /**
     * 导入Excel
     */
    upload.render({
        elem: '#btnExcel'
        , url: '/btyTxl/importWm'
        , accept: "file"
        , async: false
        , exts: 'xls|xlsx|xlsm|xlt|xltx|xltm'
        , done: function (res) {
            // if (res.state == 0) {
            //     layer.msg('导入失败！', {icon: 1, time: 2000, shade: 0.2});
            // }
            if (res == true) {
                layer.msg('导入成功！', {icon: 1, time: 2000, shade: 0.2}, function () {
                    window.location.reload();
                });
            }else {
                layer.msg('导入失败！请检查表格中日期格式是否正确！', {icon: 1, time: 2000, shade: 0.2});
            }
        },
    });


    /**
     * 点击修改
     *
     * @param data 点击按钮时候的行数据
     */
    GcgWsjd.onEditNotice = function (data) {
        func.open({
            height: 980,
            title: '修改',
            content: Feng.ctxPath + "/btyTxl/waimaiAdd?id=" + data.id,
            tableId: GcgWsjd.tableId
        });
    };

    //删除
    GcgWsjd.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/btyTxl/deleteWm", function (data) {
                Feng.success("删除成功!");
                table.reload(GcgWsjd.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 工具条点击事件
    table.on('tool(' + GcgWsjd.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'manager') {
            GcgWsjd.zhiDing(data);
        }
        if (layEvent === 'edit') {
            GcgWsjd.onEditNotice(data);
        } else if (layEvent === 'delete') {
            GcgWsjd.onDeleteItem(data);
        }
    });

});
